package com.multiplayer.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.google.gson.Gson;
import com.multiplayer.dao.IUserDao;
import com.multiplayer.dao.impl.UserDaoImpl;
import com.multiplayer.entity.User;
import com.multiplayer.service.IAdminService;
import com.multiplayer.service.IUserService;
import com.multiplayer.service.impl.AdminServiceImpl;
import com.multiplayer.service.impl.UserServiceImpl;

/**
 * 登录注册Servlet
 * @author 张文豪 2020.5.12
 *
 */
@WebServlet("/LoginRegistServlet")
public class LoginRegistServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	//业务层的依赖
    private IUserService userService = new UserServiceImpl();
    private IAdminService adminService = new AdminServiceImpl();
    IUserDao iud = new UserDaoImpl();
    
    public LoginRegistServlet() {
        super();
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String op = request.getParameter("op");
		
		if("Login".equals(op)) {
			//登录操作
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			String power = request.getParameter("power");
			boolean flag = userAdminLogin(username,password,power);
			if(flag) {
				if("student".equals(power)) {

					System.out.println("学生登录成功");
					User user = iud.queryUserById(username).get(0);
					request.getSession().setAttribute("user", user);
					response.sendRedirect("personalInfo.jsp");
				}else if("teacher".equals(power)) {
					System.out.println("管理员登录成功");
					request.getSession().setAttribute("admin",username);
					response.sendRedirect("UserManagerServlet?op=searchAll&page=1&pageSize=15");

					response.sendRedirect("学生主页");
				}else if("teacher".equals(power)) {
					System.out.println("管理员登录成功");
					response.sendRedirect("Seat.jsp");

				}
				
			}else {
				request.setAttribute("loginMsg", "登录失败");
				request.getRequestDispatcher("Login.jsp").forward(request, response);
			}
		}else if("Regist".equals(op)) {
			//注册操作
			String username = request.getParameter("username");
			String password = request.getParameter("password1");
			String power = request.getParameter("power");
			boolean flag = userAdminRegist(username,password,power);
			if(flag) {
				response.sendRedirect("Login.jsp");
			}else {
				request.setAttribute("registMsg", "注册失败");
				request.getRequestDispatcher("Regist.jsp").forward(request, response);
			}
		}else if("loginface".equals(op)) {
			//人脸识别登录
			//1.得到图片信息
			String image = request.getParameter("imag").substring(22);
			//2.调用人脸识别接口登录
			JSONObject flag = userService.userLoginByFace(image, "BASE64", "User,Admin");
			//3.将返回的结果传回界面
			
			PrintWriter out = response.getWriter();
			out.print(flag);
		}
		
		
		
	}

	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}
	/**
	 * 用户或管理员登录方法
	 * @param username 用户名
	 * @param password 密码
	 * @param power 身份
	 * @return true登录成功,false登录失败
	 */
	private boolean userAdminLogin(String username,String password,String power) {
		boolean flag = false;
		if("student".equals(power)) {
			//学生登录
			flag = userService.userLogin(username, password);
			
		}else if("teacher".equals(power)) {
			//老师登录
			
			flag = adminService.adminLogin(username, password);			
		}else {
			return false;
		}
		System.out.println("登录："+flag);
		return flag;
		
	}
	/**
	 * 用户或管理员注册方法
	 * @param username 用户名
	 * @param password 密码
	 * @param power 身份
	 * @return true注册成功，false注册失败
	 */
	private boolean userAdminRegist(String username,String password,String power) {
		boolean flag = false;
		if("student".equals(power)) {
			//学生注册
			flag = userService.userRegist(username, password);
			
		}else if ("teacher".equals(power)) {
			//老师注册
			
			flag = adminService.adminRegist(username, password);
		}
		return flag;
		
	}
	
	

}
